package com.usabb.controller;


import java.util.Arrays;

import com.usabb.domain.MaternityBaseDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.usabb.domain.HospitalDO;
import com.usabb.service.HospitalService;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 医院表
 * </pre>
 * <small> 2019-09-24 10:08:40 | Jory</small>
 */
@Controller
@RequestMapping("/usabb/hospital")
public class HospitalController extends BaseController {
	@Autowired
	private HospitalService hospitalService;
	
	@GetMapping()
	@RequiresPermissions("usabb:hospital:hospital")
	String Hospital(){
	    return "usabb/hospital/hospital";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("usabb:hospital:hospital")
	public Result<Page<HospitalDO>> list(HospitalDO hospitalDTO){
        Wrapper<HospitalDO> wrapper = new EntityWrapper<>(hospitalDTO).orderBy("id", false);
        Page<HospitalDO> page = hospitalService.selectPage(getPage(HospitalDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("usabb:hospital:add")
	String add(){
	    return "usabb/hospital/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("usabb:hospital:edit")
	String edit(@PathVariable("id") Long id,Model model){
		HospitalDO hospital = hospitalService.selectById(id);
		model.addAttribute("hospital", hospital);
	    return "usabb/hospital/edit";
	}
	
	@Log("添加医院表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("usabb:hospital:add")
	public Result<String> save( HospitalDO hospital){
		hospitalService.insert(hospital);
        return Result.ok();
	}
	
	@Log("修改医院表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("usabb:hospital:edit")
	public Result<String>  update( HospitalDO hospital){
		boolean update = hospitalService.updateById(hospital);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除医院表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("usabb:hospital:remove")
	public Result<String>  remove( Long id){
		hospitalService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除医院表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("usabb:hospital:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		hospitalService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}


    @GetMapping("/{baseId}/attach")
    public String goAttachPage(@PathVariable("baseId") Long baseId, Model model) {
        HospitalDO hospital = hospitalService.selectById(baseId);

        model.addAttribute("hospital", hospital);
        return "usabb/attachHospital/attach";
    }
}
